package com.xdtech.hotel.service.impl;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.xdtech.common.utils.EmptyUtil;
import com.xdtech.core.dao.BaseDao;
import com.xdtech.core.model.BaseItem;
import com.xdtech.core.orm.Page;
import com.xdtech.core.orm.utils.BeanUtils;
import com.xdtech.hotel.dao.CustomerDao;
import com.xdtech.hotel.model.Customer;
import com.xdtech.hotel.protocol.manager.OperationManager;
import com.xdtech.hotel.protocol.manager.OperationOption;
import com.xdtech.hotel.service.CustomerService;
import com.xdtech.hotel.service.RoomService;
import com.xdtech.hotel.util.HotelConstants;
import com.xdtech.hotel.vo.CustomerItem;
import com.xdtech.hotel.vo.RoomItem;
import com.xdtech.web.model.Pagination;

/**
 * 
 * @author max.zheng
 * @create 2015-04-04 11:08:36
 * @since 1.0
 * @see
 */
@Service
public class CustomerServiceImpl implements CustomerService {
	@Autowired
	private CustomerDao customerDao;
	@Autowired
	private BaseDao baseDao;
	@Autowired
	private RoomService roomService;

	/**
	 * @description
	 * @author max.zheng
	 * @create 2015-04-04 11:08:36
	 * @modified by
	 * @param entity
	 */
	public void save(Customer entity) {
		customerDao.save(entity);
	}
	/**
	 * @description
	 * @author max.zheng
	 * @create 2015-04-04 11:08:36
	 * @modified by
	 * @param entity
	 */
	public void saveAll(List<Customer> entities) {
		customerDao.saveAll(entities);
	}

	/**
	 * @description
	 * @author max.zheng
	 * @create 2015-04-04 11:08:36
	 * @modified by
	 * @param entity
	 */
	public void delete(Customer entity) {
		customerDao.delete(entity);
	}

	/**
	 * @description
	 * @author max.zheng
	 * @create 2015-04-04 11:08:36
	 * @modified by
	 * @param id
	 */
	public void delete(Long id) {
		customerDao.delete(id);
	}

	/**
	 * @description
	 * @author max.zheng
	 * @create 2015-04-04 11:08:36
	 * @modified by
	 * @param id
	 * @return
	 */
	public Customer get(Long id) {
		return customerDao.get(id);
	}

	/**
	 * @description
	 * @author max.zheng
	 * @create 2015-04-04 11:08:36
	 * @modified by
	 * @return
	 */
	public List<Customer> getAll() {
		return customerDao.getAll();
	}

	/**
	 * @description
	 * @author max.zheng
	 * @create 2015-04-04 11:08:36
	 * @modified by
	 * @param pg
	 * @param values
	 * @return
	 */
	public Map<String, Object> loadPageAndCondition(Pagination pg,
			Map<String, String> values) {
		Map<String, Object> results = new HashMap<String, Object>();
		List<Object> customers = null;
		long rows = 0;
		if (pg!=null) {
			Page<Customer> page = customerDao.findPage(pg,"from Customer order by createTime desc", values);
			customers = BeanUtils.copyListProperties(
					CustomerItem.class, page.getResult());
			rows = page.getTotalItems();
		}else {
			List<Customer> customerList = customerDao.find("from Customer order by id desc", values);
			customers = BeanUtils.copyListProperties(
					CustomerItem.class, customerList);
			rows = customers.size();
		}
		results.put("total", rows);
		results.put("rows", customers);
		return results;
	}

	/**
	 * @description
	 * @author max.zheng
	 * @create 2015-04-04 11:08:36
	 * @modified by
	 * @param item
	 * @return
	 */
	public boolean saveOrUpdateCustomer(CustomerItem item) {
		Customer customer = null;
		if (item.getId()==null) {
			customer = new Customer();
		}else {
			customer = customerDao.get(item.getId());
		}
		//澶嶅埗鍓嶅彴淇敼鐨勫睘鎬�
		BeanUtils.copyProperties(customer, item);
		customerDao.save(customer);
		return true;
	}

	/**
	 * @description
	 * @author max.zheng
	 * @create 2015-04-04 11:08:36
	 * @modified by
	 * @param customerId
	 * @return
	 */
	public CustomerItem loadCustomerItem(Long customerId) {
		Customer customer = customerDao.get(customerId);
		CustomerItem customerItem = new CustomerItem();
		BeanUtils.copyProperties(customerItem, customer);
		return customerItem;
	}

	/**
	 * @description
	 * @author max.zheng
	 * @create 2015-04-04 11:08:36
	 * @modified by
	 * @param id
	 * @return
	 */
	public boolean deleteCustomerInfo(long id) {
		delete(id);
		return true;
	}
	
	/**
	 * @description
	 * @author max.zheng
	 * @create 2015-04-04 11:08:36
	 * @modified by
	 * @param newIds
	 * @return
	 */
	public boolean deleteCustomerInfo(List<Long> customerIds) {
		for (Long id : customerIds) {
			delete(id);
		}
		return true;
	}
	/**
	 * @description
	 * @author max.zheng
	 * @create 2015-4-18下午12:16:47
	 * @modified by
	 * @param pg
	 * @param baseItem
	 * @param queryName
	 * @return
	 */
	@Override
	public Map<String, Object> loadPageDataByConditions(Pagination pg,
			BaseItem baseItem, String queryName) {
		Map<String, Object> results = new HashMap<String, Object>();
		Page page = baseDao.findPageByNamedQuery(pg, queryName,baseItem);
		results.put("total", page.getTotalItems());
		results.put("rows", page.getResult());
		return results;
	}
	/**
	 * @description
	 * @author max.zheng
	 * @create 2015-4-18下午9:44:28
	 * @modified by
	 * @param item
	 * @return
	 */
	@Override
	public boolean stay(CustomerItem item) {
		//更改房间状态，改成售出，同步到监控中
		RoomItem roomItem = roomService.roomUpdateStatus(item.getRoomId(),HotelConstants.ROOM_WORK_OFF_STATUS,item.getName());
		//添加顾客居住信息
		Customer customer = new Customer();
		BeanUtils.copyProperties(customer, item);
		customer.setCreateTime(new Date());
		customer.setStatus(HotelConstants.CUSTOMER_LIVING_STATUS);
		customerDao.save(customer);
		//下发卡号
		//0-管理，1-顾客，2-保洁，3-维修；
		OperationManager.sendWriteCard(roomItem.getDeviceSn(),"add","1", item.getCardNo());
		//联动命令下发
		
		return true;
	}
	/**
	 * @description
	 * @author max.zheng
	 * @create 2015-4-18下午11:41:03
	 * @modified by
	 * @param roomId
	 * @return
	 */
	@Override
	public CustomerItem loadLiveCustomerItemByRoomId(Long roomId) {
		CustomerItem customerItem = new CustomerItem();
		customerItem.setRoomId(roomId);
		List rs = baseDao.findListByNamedQuery("findCustomerByCondition",customerItem);
		return (CustomerItem) (EmptyUtil.isNotEmpty(rs)?rs.get(0):null);
	}
	/**
	 * @description
	 * @author max.zheng
	 * @create 2015-5-6下午8:52:47
	 * @modified by
	 * @param item
	 * @return
	 */
	@Override
	public boolean leave(CustomerItem item) {
		//更改房间状态，改成售出，同步到监控中
		RoomItem roomItem = roomService.roomUpdateStatus(item.getRoomId(),HotelConstants.ROOM_WAIT_CLEAR_STATUS,"");
		//添加顾客居住信息
		Customer customer = get(item.getId());
		customer.setStatus(HotelConstants.CUSTOMER_LEAVE_STATUS);
		customer.setLeaveTime(new Date());
		customerDao.save(customer);
		//删除卡号
		//0-管理，1-顾客，2-保洁，3-维修；
		OperationManager.sendWriteCard(roomItem.getDeviceSn(),"del","1", item.getCardNo());
		//联动命令下发
		return true;
	}

}
